package web.controller.my;

import framework.config.SecurityConfig;
import framework.security.AuthService;
import framework.security.LoginEntity;
import framework.vo.ResultInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import security.service.LoginService;
import security.vo.LoginInfo;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@Api(tags = "0授权管理")
@RestController
@RequestMapping("/auth/")
public class AuthController {

    @Autowired
    private AuthService authService;
    @Autowired
    private LoginService loginService;
    @Autowired
    private SecurityConfig securityConfig;

    /**
     * 登录
     *
     * @return
     */
    @PostMapping(value = "/login")
    public ResultInfo<LoginInfo> login(
            @RequestParam(required = true)
            @ApiParam(defaultValue = "admin") String username,
            @RequestParam(required = true)
            @ApiParam(defaultValue = "9dEEsDs4Oukc") String password,
            @RequestParam(required = false)
            @ApiParam(defaultValue = "", name = "passwordCipher", value = "gc") String passwordCipher,
            @RequestParam(required = false)
            @ApiParam(defaultValue = "0") Integer rememberMe,
            @RequestParam(required = false, defaultValue = "0")
            @ApiParam(defaultValue = "1") Integer cookie,
            String captchaId, String captcha,
            HttpServletRequest request,
            HttpServletResponse response) throws IOException {
        LoginEntity loginEntity = new LoginEntity();
        loginEntity.setCaptcha(captcha);
        loginEntity.setCaptchaId(captchaId);
        loginEntity.setUsername(username);
        loginEntity.setPassword(password);
        loginEntity.setPasswordCipher(passwordCipher);
        loginEntity.setRememberMe(rememberMe);
        loginEntity.setCookie(cookie);
        LoginInfo loginInfo = loginService.login(loginEntity);

        if (StringUtils.hasText(this.securityConfig.getRedirectOnAuthSuccess())) {
            response.setStatus(HttpServletResponse.SC_FOUND);
            response.setHeader("Location", this.securityConfig.getRedirectOnAuthSuccess());
        }

        return new ResultInfo<>(loginInfo);
    }

    /**
     * 登录
     *
     * @return
     */
    @GetMapping("/logout")
    public ResultInfo<Integer> logout(HttpServletResponse response, String returnUrl) throws IOException {
        loginService.logout();

        // redirectOnLogoutSuccess
        if (StringUtils.hasText(this.securityConfig.getRedirectOnLogoutSuccess())) {
            response.setStatus(HttpServletResponse.SC_FOUND);
            response.setHeader("Location", this.securityConfig.getRedirectOnLogoutSuccess());
        } else if (StringUtils.hasText(returnUrl)) {
            response.setStatus(HttpServletResponse.SC_FOUND);
            response.setHeader("Location", returnUrl);
        }

        return new ResultInfo<>(1);
    }

}
